package com.inossem.warehousestorage_inossem.adapter.outStorage;

import android.content.Context;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.out_storage.OutStorageSaleListActivity;
import com.inossem.warehousestorage_inossem.activity.out_storage.OutStorageSaleOverviewActivity;
import com.inossem.warehousestorage_inossem.activity.out_storage.extra.OutStorageExtra;
import com.inossem.warehousestorage_inossem.bean.outStorage.OutStorageSaleListBean;
import com.inossem.warehousestorage_inossem.bean.outStorage.OutStorageSaleMaterielListBean;
import com.inossem.warehousestorage_inossem.utils.OutStorageUtils;
import com.zhy.android.percent.support.PercentRelativeLayout;

import java.util.List;

/**
 * Created by leij on 2017/6/6.
 */

public class SaleOutStorageListAdapter extends BaseAdapter {

    private Context mContext;
    private List<OutStorageSaleListBean.BodyBean> mSaleOutStorageList;
    private List<OutStorageSaleMaterielListBean.BodyBean> mSaleOutStorageMaterielList;
    private LayoutInflater inflater;
    private OutStorageSaleListActivity.OutStorageSaleListener mOutStorageSaleListener;
    private String mCondition;

    public SaleOutStorageListAdapter(Context context, List<OutStorageSaleListBean.BodyBean> saleOutStorageList, List<OutStorageSaleMaterielListBean.BodyBean> saleOutStorageMaterielList, String condition, OutStorageSaleListActivity.OutStorageSaleListener outStorageSaleListener) {
        mContext = context;
        mSaleOutStorageList = saleOutStorageList;
        mSaleOutStorageMaterielList = saleOutStorageMaterielList;
        mOutStorageSaleListener = outStorageSaleListener;
        inflater = LayoutInflater.from(context);
        mCondition = condition;
    }

    @Override
    public int getCount() {
        if (mSaleOutStorageList == null) {
            return mSaleOutStorageMaterielList.size() + 1;
        } else {
            return mSaleOutStorageList.size() + 1;
        }
    }

    @Override
    public Object getItem(int position) {
        if (mSaleOutStorageList == null) {
            return mSaleOutStorageMaterielList.get(position);
        } else {
            return mSaleOutStorageList.get(position);
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (position == 0) {
            convertView = inflater.inflate(R.layout.view_out_storage_sale_list_first_item, null);
            EditText condition = (EditText) convertView.findViewById(R.id.condition);
            ImageView search = (ImageView) convertView.findViewById(R.id.search);
            TextView materiel = (TextView) convertView.findViewById(R.id.materiel);
            TextView saleOutStorageNumber = (TextView) convertView.findViewById(R.id.saleOutStorageNumber);
            View materielLine = convertView.findViewById(R.id.materielLine);
            View saleOutStorageNumberLine = convertView.findViewById(R.id.saleOutStorageNumberLine);
            if (mSaleOutStorageList == null) {
                materiel.setTextColor(ContextCompat.getColor(mContext, R.color.activity_receive_delivery_overview_font_color));
                saleOutStorageNumber.setTextColor(ContextCompat.getColor(mContext, R.color.common_font_content));
                materielLine.setVisibility(View.VISIBLE);
                saleOutStorageNumberLine.setVisibility(View.GONE);
                condition.setHint("请输入物料编码/物料描述");
            } else {
                materiel.setTextColor(ContextCompat.getColor(mContext, R.color.common_font_content));
                saleOutStorageNumber.setTextColor(ContextCompat.getColor(mContext, R.color.activity_receive_delivery_overview_font_color));
                materielLine.setVisibility(View.GONE);
                saleOutStorageNumberLine.setVisibility(View.VISIBLE);
                condition.setHint("请输入销售交货单/客户名称/联系人");
            }
            condition.setText(mCondition);
            materiel.setOnClickListener(new NoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    mOutStorageSaleListener.onChangeTab(OutStorageUtils.TAB_MATERIEL);
                }
            });
            saleOutStorageNumber.setOnClickListener(new NoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    mOutStorageSaleListener.onChangeTab(OutStorageUtils.TAB_SALE_OUT_STORAGE);
                }
            });
            search.setOnClickListener(new NoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    Utils.closeSoftKeyboard(mContext, v);
                    mOutStorageSaleListener.onNoDoubleClickListener(condition.getText().toString());
                }
            });
        } else {
            ViewHolder viewHolder;
            if (convertView == null || (convertView != null && convertView.getTag() == null)) {
                convertView = inflater.inflate(R.layout.view_out_storage_sale_list, null);
                viewHolder = new ViewHolder();
                //初始化控件
                initView(viewHolder, convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            if (mSaleOutStorageList == null) {
                initMaterielData(mContext, viewHolder, convertView, mSaleOutStorageMaterielList.get(position - 1));
            } else {
                //初始化数据
                initData(mContext, viewHolder, convertView, mSaleOutStorageList.get(position - 1));
            }

        }
        return convertView;
    }

    private void initView(ViewHolder viewHolder, View view) {
        viewHolder.saleOutStorageMaterielList = (PercentRelativeLayout) view.findViewById(R.id.saleOutStorageMaterielList);
        viewHolder.saleOutStorageList = (PercentRelativeLayout) view.findViewById(R.id.saleOutStorageList);
        viewHolder.materielCode = (TextView) view.findViewById(R.id.materielCode);
        viewHolder.materielDescription = (TextView) view.findViewById(R.id.materielDescription);
        viewHolder.number = (TextView) view.findViewById(R.id.number);

        viewHolder.saleOutStorageNumber = (TextView) view.findViewById(R.id.saleOutStorageNumber);
        viewHolder.customer = (TextView) view.findViewById(R.id.customer);
        viewHolder.contact = (TextView) view.findViewById(R.id.contact);
        viewHolder.createTime = (TextView) view.findViewById(R.id.createTime);
    }

    private void initMaterielData(Context context, ViewHolder viewHolder, View view, OutStorageSaleMaterielListBean.BodyBean saleOutStorageMaterielItem) {
        viewHolder.saleOutStorageList.setVisibility(View.GONE);
        viewHolder.saleOutStorageMaterielList.setVisibility(View.VISIBLE);
        viewHolder.materielCode.setText(saleOutStorageMaterielItem.getMat_code());
        viewHolder.materielDescription.setText(saleOutStorageMaterielItem.getMat_name());
        viewHolder.number.setText(saleOutStorageMaterielItem.getQty());
//        view.setOnClickListener(new NoDoubleClickListener() {
//            @Override
//            public void onNoDoubleClick(View v) {
//                Intent intent = new Intent(context, OutStorageSaleOverviewActivity.class);
//                intent.putExtra(OutStorageExtra.OUT_STORAGE_SALE_NUMBER, viewHolder.saleNumber.getText().toString());
//                context.startActivity(intent);
//            }
//        });
    }

    private void initData(Context context, ViewHolder viewHolder, View view, OutStorageSaleListBean.BodyBean saleOutStorageItem) {
        viewHolder.saleOutStorageMaterielList.setVisibility(View.GONE);
        viewHolder.saleOutStorageList.setVisibility(View.VISIBLE);
        viewHolder.saleOutStorageNumber.setText(saleOutStorageItem.getRefer_receipt_code());
        viewHolder.customer.setText(saleOutStorageItem.getReceive_name());
        viewHolder.contact.setText(saleOutStorageItem.getContact_person());
        viewHolder.createTime.setText(saleOutStorageItem.getShipping_time());
        view.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                Intent intent = new Intent(context, OutStorageSaleOverviewActivity.class);
                intent.putExtra(OutStorageExtra.OUT_STORAGE_SALE_NUMBER, viewHolder.saleOutStorageNumber.getText().toString());
                context.startActivity(intent);
            }
        });
    }

    private class ViewHolder {

        private PercentRelativeLayout saleOutStorageMaterielList, saleOutStorageList;

        private TextView materielCode, materielDescription, number;

        private TextView saleOutStorageNumber, customer, contact, createTime;
    }
}
